package whu.hami.tea.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import whu.hami.tea.R;
import whu.hami.tea.domain.Topic;

/**
 * Created by pendragon on 17-5-29.
 */

public class MainAdapter extends BaseAdapter {
    private Context context;
    private List<Topic> topics;

    public MainAdapter(Context context, List<Topic> topics){
        this.context = context;
        this.topics = topics;
    }

    @Override
    public int getCount() {
        return topics.size();
    }

    @Override
    public Object getItem(int position) {
        return topics.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_main, null);

            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_topic);
            viewHolder.tvBrief = (TextView) convertView.findViewById(R.id.tv_brief);
            viewHolder.imgAdd = (ImageView) convertView.findViewById(R.id.img_add);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        Topic topic = topics.get(position);
        viewHolder.tvTitle.setText(topic.topic);
        viewHolder.tvBrief.setText(topic.brief);

        return convertView;
    }

    private static class ViewHolder {
        TextView tvTitle, tvBrief;
        ImageView imgAdd;
    }
}
